﻿///
///地址级联，省/市/区
///
(function ($) {
    $.fn.addressCascade = function () {
        $(this).each(function () {
            var $this = $(this);
            var category = $this.attr('Category');
            var reference = $this.attr('ReferenceCategory');

            var parentSelect = $('select[ReferenceCategory=' + category + ']');
            var parentCategory = '';
            if (parentSelect.length > 0) {
                parentCategory = parentSelect.attr('Category');
            }
            var resetData = function () {
                $this.empty().append('<option value="-1" >请选择</option>');
                $this.selectmenu('refresh', true);

                var parentVal = parentSelect.val();
                if (parentSelect.length > 0 && (parentVal == "-1" || !parentVal)) {
                    return;
                }
                if (!parentVal) {
                    parentVal = '';
                }
                var param = { Category: category, KeyValuePairString: parentCategory + ':' + parentVal };
                $this.executeCommand({
                    successCallBack: function (result) {
                        var html = '';
                        $(result.List).each(function () {
                            html += '<option value={Key}>{Value}</option>'.format(this);
                        });
                        $this.append(html);
                        if ($this.attr('default_val') && $this.attr('default_val') != '') {
                            $this.val($this.attr('default_val'));
                        }
                        $this.selectmenu('refresh', true);
                        $this.change();
                    }
                }, 'Pub.LoadAddress', param);
            }

            $('select[ReferenceCategory=' + category + ']').change(function () {
                resetData();
            });
            resetData();

        });
    }
})(jQuery);